
package com.rt.schedulenew.web;

import com.rt.schedulebase.dto.*;
import com.rt.schedulebase.entity.DictScheduleCycle;
import com.rt.schedulenew.api.DictScheduleCycleService;
import com.rt.schedulenew.api.IExamScheduleService;
import com.rt.schedulenew.api.ISourceService;
import com.rt.schedulenew.utils.checkparam.config.Check;
import com.rt.schedulenew.utils.global.JsonResult;
import com.rt.schedulenew.utils.global.JsonResultUtil;
import com.rt.schedulenew.utils.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/source"})
public class SourceController {
    @Autowired
    ISourceService sourceService;

    @Autowired
    private DictScheduleCycleService cycleService;
    @Autowired
    private IExamScheduleService examScheduleService;

    @PostMapping({"/apm/list"})
    public JsonResult getSourceList(@RequestBody DictScheduleApmDto dto) {
        List<DictScheduleApmDto> list =  sourceService.getSourceList(dto);
        return JsonResultUtil.success("成功",  list);
    }

    @PostMapping({"/apm/startOrStopSource"})
    public JsonResult startOrStopSource(@RequestBody DictScheduleApmDto dto) {
        return JsonResultUtil.success(sourceService.startOrStopForSource(dto));
    }

    @PostMapping({"/apm/manyStartOrStopSource"})
    @Check({"resourceMark", "timeStart", "queueName", "examClass", "dateStart", "dateEnd"})
    public JsonResult manyStartOrStopSource(@RequestBody DictScheduleApmDto dto) {
        return sourceService.manyStartOrStopSource(dto);
    }

    @PostMapping({"/apm/cancelModify"})
    @Check({"resourceMark"})
    public JsonResult cancelModify(@RequestBody DictScheduleApmDto dto) {
        return JsonResultUtil.success(sourceService.cancelModify(dto));
    }

    @PostMapping({"/apm/manyCancelModify"})
    @Check({"resourceMark", "timeStart", "queueName", "examClass", "dateStart", "dateEnd"})
    public JsonResult manyCancelModify(@RequestBody DictScheduleApmDto dto) {
        return sourceService.manyCancelModify(dto);
    }

    @PostMapping({"/apm/save"})
    public JsonResult saveScheduleApm(@RequestBody DictScheduleApmDto dto) {
        return sourceService.saveScheduleApm(dto);
    }

    /**
     * 批量新增时段
     * @param dto
     * @return
     */
    @PostMapping({"/apm/batchSave"})
    public JsonResult batchSaveScheduleApm(@RequestBody DictScheduleApmBatchDto dto) {
        return sourceService.saveScheduleBatchApm(dto);
    }

    /**
     * 修改号源队列时段设置
     *
     * @param dto
     * @return
     */
    @PostMapping({"/apm/edit"})
    public JsonResult editScheduleApm(@RequestBody DictScheduleApmDto dto) {
        return sourceService.editScheduleApm(dto);
    }

    @PostMapping({"/apm/regularDefList"})
    public JsonResult getRegularDefList(@RequestBody DictScheduleRegularDefDto dto) {
        return JsonResultUtil.success("成功", sourceService.getRegularDefList(dto));
    }

    @PostMapping({"/apm/regularDefInfo"})
    public JsonResult getRegularDefInfo(@RequestBody DictScheduleApmRegularDto dto) {
        List<DictScheduleApmRegularDto> list = sourceService.getRegularDefInfo(dto);
        return JsonResultUtil.success("成功", list);
    }

    /**
     * 获取号源周期列表
     * @param cycle
     * @return
     */
    @PostMapping({"/apm/cycleList"})
    @Check({"examClass", "queueName"})
    public JsonResult getCycleList(@RequestBody DictScheduleCycle cycle) {
//        return sourceService.getCycleList(apm);
        return cycleService.getCycleList(cycle);
    }

    @PostMapping({"/apm/calendarColor"})
    public JsonResult getCalendarColor(@RequestBody DictScheduleApmDto dto) {
        Map<String, Object> map = sourceService.getCalendarColor(dto);
        return JsonResultUtil.success("成功", map);
    }

    @PostMapping({"/apm/maxLimit"})
    public JsonResult updateScheduleApmMaxLimit(@RequestBody DictScheduleApmDto apm) {
        return sourceService.updateScheduleApmMaxLimit(apm);
    }

    @PostMapping({"/apm/batchModify"})
    @Check({"apmCode", "queueName", "examClass"})
    public JsonResult batchModifySchedule(@RequestBody DictScheduleApmDto apm) {
        return sourceService.batchModifySchedule(apm);
    }

    @PostMapping({"/apm/manyBatchModify"})
    @Check({"resourceMark", "timeStart", "queueName", "examClass", "dateStart", "dateEnd"})
    public JsonResult manyBatchModifySchedule(@RequestBody DictScheduleApmDto apm) {
        return sourceService.manyBatchModifySchedule(apm);
    }

    @PostMapping({"/apm/manylist"})
    @Check({"calendarDate", "queueName", "examClass", "dateStart", "dateEnd"})
    public JsonResult getManySourceList(@RequestBody DictScheduleApmDto dto) {
        return sourceService.getManySourceList(dto);
    }


    /**
     * 保存号源周期
     * @param cycle
     * @return
     */
    @PostMapping({"/cycle/saveJudge"})
    @Check({"examClass", "queueName", "dateStart", "dateEnd", "cycleType","cycleName"})
    public JsonResult saveRangeJudge(@RequestBody DictScheduleCycle cycle) {
        return cycleService.saveRangeJudge(cycle);
    }

//    @PostMapping({"/cycle/editJudge"})
//    public JsonResult edit(@RequestBody DictScheduleApmDto apm) {
//        return sourceService.editRangeJudge(apm);
//    }

    @PostMapping({"/cycle/deleteJudge"})
    @Check({"id"})
    public JsonResult delete(@RequestBody DictScheduleCycle cycle) {
//        return sourceService.deleteRangeJudge(apm);
        return cycleService.deleteRangeJudge(cycle);
    }

    @PostMapping({"/apm/copy"})
    @Check({"apmCode", "queueName", "scheduleDate"})
    public JsonResult copyDictScheduleApm(@RequestBody DictScheduleApmDto apm) {
        return sourceService.copyDictScheduleApm(apm);
    }

    @PostMapping({"/apm/getScheduleDetail"})
    @Check({"examClass", "queueName", "endDate", "startDate"})
    public JsonResult getScheduleDetail(@RequestBody ExamScheduleDto dto) {
        Map<String, Object> mapResult = examScheduleService.getScheduleQueueDetailSum(dto);
        List<String> apmSumList = (List<String>) mapResult.get("apmSum");
        List<String> dateList = DateUtil.findDaysStr(dto.getStartDate(), dto.getEndDate());
        List<Map> list = new ArrayList<>();
        for (int i = 0; i < dateList.size(); ++i) {
            String sourceTotal = apmSumList.get(i);
            String[] arr = sourceTotal.split("/");
            if (arr.length > 2) {
                sourceTotal = arr[1] + "/" + arr[2];
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("scheduleDate", dateList.get(i));
            map.put("sourceTotal", sourceTotal);
            list.add(map);
        }
        return JsonResultUtil.success("成功", (Object) list);
    }

    @PostMapping({"/apm/sourceTotalEdit"})
    @Check({"examClass", "queueName", "dateStart", "dateEnd", "maxLimit"})
    public JsonResult sourceTotalEdit(@RequestBody DictScheduleApmDto apm) {
        return sourceService.sourceTotalEdit(apm);
    }

    @PostMapping({"/wholeApm/calendarColor"})
    @Check({"examClass", "calendarDate", "dateStart", "dateEnd", "timeStart", "timeEnd"})
    public JsonResult getWholeCalendarColor(@RequestBody DictScheduleApmDto dto) {
        Map<String, Object> map = sourceService.getWholeCalendarColor(dto);
        return JsonResultUtil.success("成功", (Object) map);
    }

    @PostMapping({"/wholeApm/list"})
    @Check({"examClass", "queueName", "calendarDate", "dateStart", "dateEnd", "timeStart", "timeEnd", "apmType"})
    public JsonResult getWholeSourceList(@RequestBody DictScheduleApmDto dto) {
        return sourceService.getWholeSourceList(dto);
    }

    @PostMapping({"/apm/scheduleApmByCycle"})
    @Check({"dateStart", "dateEnd", "queueName"})
    public JsonResult getScheduleApmByCycle(@RequestBody DictScheduleApmDto apm) {
        return sourceService.getScheduleApmByCycle(apm);
    }

    @PostMapping({"/apm/getCycleToPage"})
    @Check({"examClass","pageSize","pageNo"})
    public JsonResult getCycleToPage(@RequestBody DictScheduleCycleDto dto) {
        return cycleService.getCycleToPage(dto);
    }

    @PostMapping({"/apm/enableOrDisable"})
    @Check({"stopFlag", "apmCode"})
    public JsonResult enableOrdisable(@RequestBody DictScheduleApmDto apm) {
        return sourceService.batchEnableOrDisable(apm);
    }

    @PostMapping({"/apm/manyDateEnableOrdisable"})
    @Check({"stopFlag", "apmCode", "scheduleDate"})
    public JsonResult manyDateEnableOrdisable(@RequestBody DictScheduleApmDto apm) {
        return sourceService.manyDateEnableOrdisable(apm);
    }

    @PostMapping({"/apm/updateScheduleApm"})
    @Check({"stopFlag", "apmCode"})
    public JsonResult updateScheduleApm(@RequestBody DictScheduleApmDto apm) {
        sourceService.updateScheduleApm(apm);
        return JsonResultUtil.success();
    }

    @PostMapping({"/apm/batchUpdateScheduleApm"})
    @Check({"apmCode"})
    public JsonResult batchUpdateScheduleApm(@RequestBody DictScheduleApmDto apm) {
        return sourceService.batchUpdateScheduleApm(apm);
    }

    @PostMapping({"/wholeApm/cancelModify"})
    @Check({"examClass", "queueName", "dateStart", "dateEnd", "timeStart", "timeEnd"})
    public JsonResult wholeCancelModify(@RequestBody DictScheduleApmDto dto) {
        return sourceService.wholeCancelModify(dto);
    }

    @PostMapping({"/apm/batchUpdateBigScheduleApm"})
    @Check({"mergeApmCode"})
    public JsonResult batchUpdateBigScheduleApm(@RequestBody DictScheduleApmDto apm) {
        return sourceService.batchUpdateBigScheduleApm(apm);
    }

    @PostMapping({"/apm/batchCopyBigScheduleApm"})
    @Check({"apmCode", "scheduleDate", "resourceMark"})
    public JsonResult batchCopyBigScheduleApm(@RequestBody DictScheduleApmDto apm) {
        return sourceService.batchCopyBigScheduleApm(apm);
    }

    @PostMapping({"/apm/batchModifyMinSchedule"})
    @Check({"apmCode", "queueName"})
    public JsonResult batchModifyMinSchedule(@RequestBody DictScheduleApmDto apm) {
        return sourceService.batchModifyMinSchedule(apm);
    }
}
